import baseapi from '@/api/base'
import { getDataByParameter, getObjetInfoByPid, getCountByParameter, saveObjetInfo, deleteObjetInfo ,postInfo} from '@/utils/baserequest'
import storage from 'store'

const Base64 = require('js-base64').Base64

const api = {
  appContext: baseapi.biz,
  appAppId: baseapi.biz_app,
  appServer: 'purchaseOrder',
  appServer2: 'purchaseOrderDetail',
  
  // saveInfo: 'purchaseOrder/',
  //推送订单
  pushOrder:'scm/purOrderToSaleOrder',
  getByConditions: 'purchaseOrder/by/findLikeExample',
  getByConditionsCount: 'purchaseOrder/countLikeExample/',
  //保存订单商品信息
  saveOederGoods:'purchaseOrderDetail/batchEdit',
  editOederGoods:'purchaseOrderDetail/batchEdit',
  getDetailByConditions: 'purchaseOrderDetail/by/findLikeExample',
  getDetailByConditionsCount: 'purchaseOrderDetail/countLikeExample/',

}
export default api

//多条件查询
export function getByConditions(parameter, pagination, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const pa = {
    example: Base64.encode(JSON.stringify(Object.assign( parameter,  {orgPid: storage.get('org') }))),
    pageSize: pagination.pageSize,
    currentPage: pagination.current,
    sidx: "",
    order: "0"
  }
  const url = api.appContext + api.getByConditions
  const allheaders = appIdHeaders
  return getDataByParameter(url, pa, allheaders)
}
//多条件查询列表条数
export function getByConditionsCount(parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const example = Object.assign( parameter,  {orgPid: storage.get('org') })
  const url = api.appContext + api.getByConditionsCount
  const allheaders = appIdHeaders
  return getCountByParameter(url, Base64.encode(JSON.stringify(example)), allheaders)
}
//新增和修改
export function saveInfo(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer + '/'
  const allheaders = appIdHeaders
  return saveObjetInfo(url, parameter, allheaders)
}
export function deleteInfo(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer + '/'
  const allheaders = appIdHeaders
  return deleteObjetInfo(url, parameter, allheaders)
}
// 推送订单
export function pushOrder(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.pushOrder
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}
// 通过pid得到信息通用请求:get请求
export function getInfoByPid(parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer
  const allheaders = appIdHeaders
  return getObjetInfoByPid(url, parameter, allheaders)
}

//删除订单单个商品信息
export function deleteGoodsInfo(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.appServer2 + '/'
  const allheaders = appIdHeaders
  return deleteObjetInfo(url, parameter, allheaders)
}
//保存订单商品信息 
export function saveOederGoods(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.saveOederGoods
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}

//保存订单商品信息 
export function editOederGoods(parameter) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org')
  }
  const url = api.appContext + api.editOederGoods
  const allheaders = appIdHeaders
  return postInfo(url, parameter, allheaders)
}

//多条件查询明细商品
export function getDetailByConditions(parameter, pagination, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const pa = {
    example: Base64.encode(JSON.stringify(Object.assign( parameter,  {orgPid: storage.get('org') }))),
    pageSize: pagination.pageSize,
    currentPage: pagination.current,
    sidx: "",
    order: "0"
  }
  const url = api.appContext + api.getDetailByConditions
  const allheaders = appIdHeaders
  return getDataByParameter(url, pa, allheaders)
}
//多条件查询列表条数
export function getDetailByConditionsCount(parameter, headers) {
  const appIdHeaders = {
    appId: api.appAppId,
    org: storage.get('org'),
    authOption: (headers && headers.authOption && headers.authOption !== 'undefined') ? headers.authOption : '1'
  }
  const example = Object.assign( parameter,  {orgPid: storage.get('org') })
  const url = api.appContext + api.getDetailByConditionsCount
  const allheaders = appIdHeaders
  return getCountByParameter(url, Base64.encode(JSON.stringify(example)), allheaders)
}








